package ee.greaton.kultuuriaken;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SettingsDBAdapter {

	public static final String KEY_MAAKOND = "maakond";
	public static final String KEY_UPDATE = "updateDate";
	public static final String KEY_DAYS = "daysAhead";
	public static final String KEY_HIDE1 = "hidePast";
	public static final String KEY_THEME = "theme";
	public static final String KEY_GUIDE = "guide";
	private static final String DATABASE_TABLE = "seade";
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + "(maakond integer,"
			+ " updateDate text not null, daysAhead integer not null, hidePast integer not null, theme integer not null, guide integer not null);";
	private DatabaseHelper DBHelper;
	SQLiteDatabase db;

	public SettingsDBAdapter(Context context) {
		DBHelper = new DatabaseHelper(context, DATABASE_TABLE,
				DATABASE_VERSION, DATABASE_CREATE);
	}

	public SettingsDBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		DBHelper.close();
	}

	public void updateSetting(int maakond) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_MAAKOND, maakond);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
	
	public void updateDate() {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		String date = year + "." + month + "." + day;
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_UPDATE, date);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
	
	public void updateDays(int days){
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_DAYS, days);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
	
	public void updateHide1(boolean hide){
		ContentValues initialValues = new ContentValues();
		int res;
		if(hide){
			res = 1;
		}else{
			res = 0;
		}
		initialValues.put(KEY_HIDE1, res);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
	
	public void addSetting(int maakond, int days){
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_MAAKOND, maakond);
		initialValues.put(KEY_UPDATE, "0.0.0000");
		initialValues.put(KEY_DAYS, days);
		initialValues.put(KEY_HIDE1, 0);
		initialValues.put(KEY_THEME, 0);
		initialValues.put(KEY_GUIDE, 0);
		db.insert(DATABASE_TABLE, null, initialValues);
	}

	public int getCount() {
		Cursor cursor = getSetting();
		int count = cursor.getCount();
		cursor.close();
		return count;
	}

	public Cursor getSetting() {
		String[] a = {KEY_MAAKOND, KEY_UPDATE, KEY_DAYS, KEY_HIDE1, KEY_THEME, KEY_GUIDE};
		return db.query(DATABASE_TABLE, a, null, null, null, null, null);
	}

	public void updateTheme(int newTheme) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_THEME, newTheme);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
	
	public void updateGuide(int newGuide) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_GUIDE, newGuide);
		db.update(DATABASE_TABLE, initialValues, null, null);
	}
}
